# -*- encoding=UTF-8 -*-

from ganstore import app, db
from flask_script import Manager
from ganstore.models import User, Rankrecord, Seller, Tread, Image, Policyrecord, PolicyImage
import random
from datetime import datetime
import unittest

manager = Manager(app)


def get_image_url():
    return 'http://images.nowcoder.com/head/' + str(random.randint(0, 1000)) + 'm.png'


def get_phone():
    return '1517245' + str(random.randint(0, 9000) + 1000)


def get_price():
    return random.randint(0, 1000) + 100


def get_tread_wide():
    return random.randint(0, 50) + 100


def get_flat_radio():
    return random.randint(0, 10) + 50


def get_diameter():
    return random.randint(0, 10) + 90


def get_car_num():
    return random.randint(0, 10000) + 10000


def get_policy_event():
    arr = ['审核中', '审核成功', '审核失败']
    return arr[random.randint(0, 2)]


@manager.command
def run_test():
    tests = unittest.TestLoader().discover('./')
    unittest.TextTestRunner().run(tests)
    pass


@manager.command
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('User' + str(i + 1), 'a' + str(i), get_phone()))
        for j in range(0, 5):
            db.session.add(Rankrecord('积分获取', '签到', '+100', i + 1))
        for k in range(0, 3):
            db.session.add(Policyrecord('鄂A' + str(get_car_num()), get_policy_event(), i + 1, str(datetime.now())))
            for m in range(0, 3):
                db.session.add(PolicyImage(get_image_url(), i * 3 + k + 1))

    for i in range(0, 100):
        db.session.add(Seller('八戒轮胎服务店' + str(i + 1), '湖北武汉', '用户' + str(i + 1), get_phone()))
        for j in range(0, 10):
            db.session.add(
                Tread('米其林轮胎' + str(i * 10 + j + 1), get_price(), get_tread_wide(), get_flat_radio(), get_diameter(),
                      i + 1))
            for k in range(0, 4):
                db.session.add(Image(get_image_url(), i * 10 + j + 1))
    db.session.commit()


if __name__ == '__main__':
    manager.run()
